package com.gongpingjialoan.activity.car;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.main.BaseActivity;

/**
 * Created by Administrator on 2016/5/13.
 */
public class EvalActivity extends BaseActivity {

    private ViewPager mViewPager;

    private EvalFragment mEvalFragment;

    private EvalFragment mEvalBuyFragment;

    private RadioGroup mRadioGroup;

    private RadioButton mRadioButton , mRadioButton1;

    private MyViewPageAdapter mMyViewPageAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isSendData = true;
        action = "my_evalRecord";
        super.onCreate(savedInstanceState);
        setContentView(R.layout.eval_layout);
        setTitle("估值记录");
        mViewPager = (ViewPager) findViewById(R.id.viewpage);
        mRadioGroup = (RadioGroup) findViewById(R.id.title_tab);
        mRadioButton = (RadioButton) findViewById(R.id.radio_new);
        mRadioButton1 = (RadioButton) findViewById(R.id.radio_experts);
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.radio_new) {
                    if (mViewPager.getCurrentItem() != 0) {
                        mViewPager.setCurrentItem(0);
                    }
                } else {
                    if (mViewPager.getCurrentItem() != 1) {
                        mViewPager.setCurrentItem(1);
                    }
                }
            }
        });
        mEvalFragment = new EvalFragment(0);
        mEvalBuyFragment = new EvalFragment(1);
        mMyViewPageAdapter = new MyViewPageAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mMyViewPageAdapter);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    mRadioButton.setChecked(true);
                } else {
                    mRadioButton1.setChecked(true);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private class MyViewPageAdapter extends FragmentPagerAdapter{

        public MyViewPageAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            if(position==0){
                return  mEvalFragment;
            }else{
                return mEvalBuyFragment;
            }
        }

        @Override
        public int getCount() {
            return 2;
        }
    }

}
